package com.lyf.baoxian.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.lyf.baoxian.R;
import com.lyf.baoxian.activity.MainActivity;
import com.lyf.baoxian.activity.MainActivity1;

import butterknife.ButterKnife;

/**
 * Created by liuyafei on 2016/11/30.
 * Fragment的基类
 */
public abstract class BaseFragment extends Fragment {
    protected MainActivity1 mActivity;
    //根布局
    private View mViewRoot;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (MainActivity1) activity;
    }


    /**
     * 创建布局
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        //判断是否为空，为空的时候去加载布局，onCreateView在界面切换的时候被多次调用，防止界面跳转回来的时候显示空白
        if (mViewRoot == null){
           // mViewRoot = createView(inflater,container,savedInstanceState);
            mViewRoot = addLayout(inflater);
        }

        ButterKnife.bind(this,mViewRoot);
        afterCreate(savedInstanceState);
        return mViewRoot;
    }

    /**
     * 加载布局后调用的方法
     * @param savedInstanceState
     */
    protected abstract void afterCreate(Bundle savedInstanceState);

    /**
     * 添加布局
     * @param inflater
     * @return
     */
    protected abstract View addLayout(LayoutInflater inflater);

    /**
     * 当界面被切换出去调用的方法，解决ViewGroup只有一个子View的bug
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mViewRoot != null){
            ViewParent parent = mViewRoot.getParent();
            if (parent instanceof ViewGroup){
                ViewGroup viewGroup = (ViewGroup) parent;
                viewGroup.removeView(mViewRoot);
            }
        }
    }

    /**
     * 每个fragment要现实的根布局不一样，所以用抽象方法去加载布局
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     *//*
    abstract protected  View createView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState);*/

    /**
     * Fragment切换到方法
     * @param fragment
     * @param addToBackStack 是否添加到回退栈
     */
    public void swichToChildFragment(Fragment fragment, boolean addToBackStack){
        //获取Fragment的管理器
        FragmentManager fragmentManager = getFragmentManager();
        //事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (addToBackStack){  //添加到回退栈
            transaction.addToBackStack(fragment.getTag());
        }
        transaction.replace(R.id.fl_container,fragment);
        transaction.commit();
    }

    /**
     * 切换到底部导航的fragment
     */
    /*public void switchNavigationFragment(int checkedId){
        mActivity.switchNavigationFragment(checkedId);
    }*/
}
